package znbkListNtxl.presenter;

import android.util.Log;

import znbkListNtxl.api.TrainingDifficultProblemsListApi;
import znbkListNtxl.entity.TrainingDifficultProblemsListEntity;
import znbkListNtxl.ITrainingDifficultProblemsPresenter;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import znbkListNtxl.fragment.TrainingDifficultProblemsListFragment;

import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

import java.util.List;

import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;
import znbkTestPaperNtxl.activity.TrainingDifficultProblemsPaperActivity;

/**
 * Created by Administrator on 2018\2\1 0001.
 */

public class TrainingDifficultProblemsListPresenter implements ITrainingDifficultProblemsPresenter {
    private ILoadMoreDataView view;

    private int status = -1;//题型的状态
    private String queType = "";//默认所有题型
    private int pagIndex = 1;

    private int lastPosition = 0;

    private ProgressSubscriber mSubscriber;


    private HttpOnNextListener<TrainingDifficultProblemsListEntity> listener = new HttpOnNextListener<TrainingDifficultProblemsListEntity>() {
        @Override
        public void onNext(TrainingDifficultProblemsListEntity trainingDifficultProblemsListEntity) {
            if (trainingDifficultProblemsListEntity.getTotalQues() > 0) {
                view.hideLoading();
                List<TrainingDifficultProblemsListEntity.QuesItemListBean> modelList = trainingDifficultProblemsListEntity.getQuesItemList();
                for (int i = 0 ; i < modelList.size() ; i ++){
                    //预处理题库返回不合理的地方
                    String summary = modelList.get(i).getQuesBrief();

                    Log.e("==HttpOnNextListener=0=","ii==="+i+"summary==="+summary);
                   /* summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/

                   if(summary!=null) {
                       String regexStr = "<[^>]*>"; //去除所有的标签
                       summary = summary.replaceAll(regexStr, "");
                       if (summary.length() > 100) {
                           summary = summary.substring(0, 100);
                       }
                   }else{
                       summary = "已删除";
                   }
                    modelList.get(i).setQuesBrief(summary);
//                    modelList.get(i).setQuesBrief(String.valueOf(i));
                }

                if (pagIndex == 1) {
                    view.loadData(trainingDifficultProblemsListEntity);
                    lastPosition = 0;
                }else {
                    view.loadMoreData(trainingDifficultProblemsListEntity);
                }

            }else {
                view.showNoData("");
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.showNetError();
        }
    };

    public TrainingDifficultProblemsListPresenter(ILoadMoreDataView view) {
        this.view = view;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        TrainingDifficultProblemsListApi api = new TrainingDifficultProblemsListApi(listener,(TrainingDifficultProblemsListFragment)view);
        api.setAnswerState(status);
        api.setQueType(queType);
        api.setPageNumber(pagIndex);
        api.setPageSize(10+lastPosition);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }

    @Override
    public void getData(boolean isShowing) {
        if (isShowing) view.showLoading();
        pagIndex = 1;
        getData();

    }

    @Override
    public void getMoreData() {
        pagIndex = pagIndex +1;
        getData();
    }

    @Override
    public void getDataByStatus(int quesStatusId) {
//        if (status == quesStatusId) return;
        status = quesStatusId;
        queType = "";
        getData(false);
    }

    @Override
    public void getDataByQuesType(String quesTypeId) {
//        if (queType.equals(quesTypeId)) return;
        queType = quesTypeId;
        getData(false);
    }

    public void openItemDetail(int position, TrainingDifficultProblemsListEntity.QuesItemListBean quesItemListBean) {
        lastPosition = position;
//        //TODO 调用下面方法进行跳转
        ZnbkPaperInfor.AnswerState = quesItemListBean.getAnswerState();
        ZnbkPaperInfor.QuesID = quesItemListBean.getQuesID();
        TrainingDifficultProblemsPaperActivity.launch((BaseFragment) view,quesItemListBean, ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS);
        Log.e("========QuesID======",""+quesItemListBean.getQuesID());
    }
}